<?php
declare (strict_types=1);

namespace app\exam\model\admin;

use app\common\model\payment\admin\PaymentOrderItem;
use app\common\model\payment\admin\PaymentTransaction;
use app\school\model\school\School;
use app\user\logic\user\Account;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * Class Order
 *
 * @package app\exam\model\admin
 */
class Order extends PaymentOrderItem
{
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/5/6
     *
     * @param $query
     */
    public function scopeMhm($query)
    {
        // 如果不是平台时, 加入机构条件限制
        if (!request()->is_platform) {
            $query->where('__TABLE__.mhm_id', request()->mhm_id);
        }
    }

    /**
     * 交易记录
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/5/6
     *
     * @return mixed|HasOne
     */
    public function transaction()
    {
        return $this->hasOne(PaymentTransaction::class, 'order_no', 'order_no');
    }

    /**
     * 机构
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/5/6
     *
     * @return BelongsTo
     */
    public function school(): BelongsTo
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_name' => 'title']);
    }

    /**
     * 购买者
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/5/6
     *
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id');
    }

    /**
     * examinationTitle
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/14
     *
     * @return BelongsTo
     */
    public function examinationTitle(): BelongsTo
    {
        return $this->belongsTo(ModuleExamination::class, 'product_id')->with(['paper_title'])->bind(['product_title' => 'title']);
    }

    /**
     * specialTitle
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/14
     *
     * @return BelongsTo
     */
    public function specialTitle(): BelongsTo
    {
        return $this->belongsTo(ModuleSpecial::class, 'product_id')->bind(['product_title' => 'title']);
    }

    /**
     * rollupTitle
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/14
     *
     * @return BelongsTo
     */
    public function rollupTitle(): BelongsTo
    {
        return $this->belongsTo(ModuleRollup::class, 'product_id')->bind(['product_title' => 'title']);
    }

}